/**
 * @description 角色编辑弹窗
 * @author 李艳
 */

import React, {Component} from 'react'
import {Form, Input, Checkbox, Modal,Select} from 'antd';
import {checkInputCode, getUserInfo} from "../../../commons/utils/CommonUtils";
import TreeSelectWithService from "../../../commons/components/TreeSelectWithService";
import {organizationConfig, userTypeConfig} from "../../../configs/CommonComponentsConfig";
import SelectWithService from "../../../commons/components/SelectWithService";

const FormItem = Form.Item;

class AddOrEditRoleModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isPublicRole: false,
      code: "",
      defaultValue: null
    }
  }

  handleClose = () => {
    this.props.form.resetFields();
    this.setState({isPublicRole: false, code: ""})
  };

  componentDidMount() {
    this.props.onRef(this);
  }

  onPublicRoleChange = (rule, value, callback) => {
    this.setState({isPublicRole: value})
    callback()
  };
  onCodeChange = (e) => {
    this.setState({code: e.target.value})
  };

  render() {
    const formItemLayout = {
      labelCol: {
        span: 7
      },
      wrapperCol: {
        span: 15
      },
    };

    const {confirmLoading, modalVisible, handleOk, handleCancel, defaultValue, isEdit, roleGroup,title} = this.props;
    const {getFieldDecorator, getFieldValue} = this.props.form;
    // let title = "编辑功能角色";
    let FormValue = defaultValue;
    let code= defaultValue.code ? defaultValue.code.substring(defaultValue.code.indexOf('-') + 1, defaultValue.code.length) : "";
    if (!isEdit) {
      // title = "新增功能角色";
      FormValue = {};
    }
    const {tenantCode = ""} = getUserInfo();
    return (
      <div>
        <Modal title={title}
                       visible={modalVisible}
                       onOk={handleOk}
                       onCancel={handleCancel}
                       width={500}
                       afterClose={this.handleClose}
                       confirmLoading={confirmLoading}
                       maskClosable={false}
        >
          <FormItem
            style={{display: "none"}}
            label="featureRoleGroup.id">
            {getFieldDecorator('featureRoleGroup.id', {
              initialValue: roleGroup.id ? roleGroup.id : "",
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            style={{display: "none"}}
            label="id">
            {getFieldDecorator('id', {
              initialValue: FormValue.id ? FormValue.id : "",
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            label={"代码"}
            {...formItemLayout}
          >
            {getFieldDecorator('code', {
              initialValue: code,
              rules: [{required: true, message: '请填写代码!'},checkInputCode()]
            })(
              <Input addonBefore={`${tenantCode}-`}/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="名称">
            {getFieldDecorator('name', {
              initialValue: FormValue.name ? FormValue.name : "",
              rules: [{required: true, message: '请填写名称!'}]
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            label={"角色类型"}
            {...formItemLayout}
          >
            {getFieldDecorator('roleType', {
              initialValue: isEdit && FormValue ? FormValue.roleType : "CanUse",
              rules: [{required: true, message: '请选择角色类型!'}]
            })(
              <Select>
                <Select.Option value="CanUse">可以使用</Select.Option>
                <Select.Option value="CanAssign">可以分配</Select.Option>
              </Select>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="公共角色">
            {getFieldDecorator('isPublicRole', {
              valuePropName: 'checked',
              initialValue: !!(FormValue.publicOrg && FormValue.publicUserType),
              rules: [{required: false}, {validator: this.onPublicRoleChange}]
            })(
              <Checkbox/>
            )}
          </FormItem>
          {getFieldValue("isPublicRole") && <FormItem
            {...formItemLayout}
            label="公共角色组织机构">
            {getFieldDecorator('publicOrg.id', {
              initialValue: FormValue.publicOrg ? FormValue.publicOrg.id : "",
              rules: [{required: true, message: '请选择公共角色组织机构!'}]
            })(
              <TreeSelectWithService config={organizationConfig}/>
            )}
          </FormItem>}
          {getFieldValue("isPublicRole") && <FormItem
            {...formItemLayout}
            label="公共角色用户类型">
            {getFieldDecorator('publicUserType', {
              initialValue: FormValue.publicUserType ? FormValue.publicUserType : "",
              rules: [{required: true, message: '请选择公共角色用户类型!'}]
            })(
              <SelectWithService config={userTypeConfig} initValue={false}/>
            )}
          </FormItem>}
        </Modal>
      </div>
    );
  }
}

AddOrEditRoleModal = Form.create()(AddOrEditRoleModal);
export default AddOrEditRoleModal;
